package com.bw.widget.design

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import java.util.*

class MonthView(ctx:Context,attrs: AttributeSet) : View(ctx,attrs) {

    private var textSize : Int = 0
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var monthStr : String
    private var circleX = 0f
    private var circleY = 0f
    private var radius = 0f
    private var spacing = 0

    fun setTime(str : String){
        monthStr = str
        invalidate()
    }

    init {
        val calendar = Calendar.getInstance()
        monthStr = ""+(calendar.get(Calendar.MONTH) + 1)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        textSize = (measuredWidth / 4).coerceAtMost(measuredHeight / 4)
        radius = ((measuredWidth).coerceAtMost(measuredHeight)/2).toFloat()
        circleX = (measuredWidth/2).toFloat()
        circleY = (measuredHeight/2).toFloat()
        spacing = (radius/10).toInt()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas!!.let {
            paint.color = Color.WHITE
            it.drawCircle(circleX,circleY,radius,paint)
            paint.color = Color.RED
            it.drawCircle(circleX,circleY,(radius-spacing),paint)
            paint.color = Color.WHITE
            paint.textAlign=Paint.Align.CENTER
            paint.textSize = textSize.toFloat()
            it.drawText(monthStr, circleX,circleY+textSize.toFloat()/2,paint)
        }


    }

}